package com.cloudknow.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * 创建日期：2018/11/19 on 13:33
 * 描述:
 * 作者:ShuY HM11
 */
public class CommonDialog extends AlertDialog {
    /**
     * 外部传入view，这样可以随意定制
     */
    private View view;
    /**
     * 上下文，必有
     */
    private Context mContext;
    /**
     * 外部点击是否可以隐藏dialog，通过onCreate中的setCancelable(cancelTouchout)方法来控制
     * 为true代表可以消失，如果传入的为false则代表不能消失
     */
    private boolean cancelTouchout;

    private int height;
    private int width;

    protected CommonDialog(Builder builder) {
        super(builder.mContext);
        mContext = builder.mContext;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    private CommonDialog(Builder builder, int resStyle) {
        super(builder.mContext, resStyle);
        mContext = builder.mContext;
        height = builder.height;
        width = builder.width;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        setContentView(view);
        setCanceledOnTouchOutside(cancelTouchout);
        setCancelable(cancelTouchout);

    }

    public static final class Builder {
        private Context mContext;
        private int height, width;
        private boolean cancelTouchout;
        private View view;
        private String content;
        private int resStyle = -1;

        public Builder(Context context) {
            this.mContext = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(mContext).inflate(resView, null);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes, View.OnClickListener listener) {
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }

        public Builder setTitle(int viewRes, String title) {
            TextView txtTitle = (TextView) view.findViewById(viewRes);
            txtTitle.setText(title);
            return this;
        }

        public Builder setMessage(int viewRes, String message) {
            TextView txtMessage = (TextView) view.findViewById(viewRes);
            txtMessage.setText(message);
            return this;
        }

        public CommonDialog build() {
            if (resStyle != -1) {
                return new CommonDialog(this, resStyle);
            } else {
                return new CommonDialog(this);
            }
        }
    }
}
